package cn.iocoder.yudao.module.erp.service.purchasemanage;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import cn.iocoder.yudao.module.erp.controller.admin.purchasemanage.vo.PurchaseManagePageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchasemanage.vo.PurchaseManageSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchasemanage.PurchaseManageDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseplan.PurchasePlanDO;
import cn.iocoder.yudao.module.erp.dal.mysql.purchasemanage.PurchaseManageMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.purchaseplan.PurchasePlanMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PURCHASE_MANAGE_NOT_EXISTS;

/**
 * 采购管理主 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class PurchaseManageServiceImpl implements PurchaseManageService {

    @Resource
    private PurchaseManageMapper purchaseManageMapper;
    @Resource
    private BpmProcessInstanceApi processInstanceApi;
    @Value("${task.purchase_manage.key:purchase_manage}")
    private String purchaseManageTask;
    @Resource
    private PurchasePlanMapper purchasePlanMapper;

    @Override
    @Transactional
    public Long createPurchaseManage(PurchaseManageSaveReqVO createReqVO) {
        // 插入
        PurchaseManageDO purchaseManage = BeanUtils.toBean(createReqVO, PurchaseManageDO.class);
        purchaseManageMapper.insert(purchaseManage);

        //插入工作流
        Map<String, Object> processInstanceVariables = new HashMap<>();
        processInstanceVariables.put("purchaseManageId", purchaseManage.getId());
        processInstanceVariables.put("purchaseManagePlanName", purchaseManage.getPlanName());
        processInstanceVariables.put("purchaseManagePlanNo", purchaseManage.getPurchasePlanNo());
        processInstanceVariables.put("purchaseManageBudget", purchaseManage.getPurchaseBudget());
        processInstanceVariables.put("purchaseManageMethod", purchaseManage.getPurchaseMethod());
        String processInstanceId = processInstanceApi.createProcessInstance(getLoginUserId(),
                new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(purchaseManageTask)
                        .setVariables(processInstanceVariables).setBusinessKey(String.valueOf(purchaseManage.getId()))
        );
        PurchasePlanDO update = new PurchasePlanDO();
        update.setId(purchaseManage.getId());
        update.setProcessInstanceId(processInstanceId);
        purchasePlanMapper.updateById(update);

        // 返回
        return purchaseManage.getId();
    }

    @Override
    public void updatePurchaseManage(PurchaseManageSaveReqVO updateReqVO) {
        // 校验存在
        validatePurchaseManageExists(updateReqVO.getId());
        // 更新
        PurchaseManageDO updateObj = BeanUtils.toBean(updateReqVO, PurchaseManageDO.class);
        purchaseManageMapper.updateById(updateObj);
    }

    @Override
    public void deletePurchaseManage(Long id) {
        // 校验存在
        validatePurchaseManageExists(id);
        // 删除
        purchaseManageMapper.deleteById(id);
    }

    @Override
        public void deletePurchaseManageListByIds(List<Long> ids) {
        // 校验存在
        validatePurchaseManageExists(ids);
        // 删除
        purchaseManageMapper.deleteByIds(ids);
        }

    private void validatePurchaseManageExists(List<Long> ids) {
        List<PurchaseManageDO> list = purchaseManageMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PURCHASE_MANAGE_NOT_EXISTS);
        }
    }

    private void validatePurchaseManageExists(Long id) {
        if (purchaseManageMapper.selectById(id) == null) {
            throw exception(PURCHASE_MANAGE_NOT_EXISTS);
        }
    }

    @Override
    public PurchaseManageDO getPurchaseManage(Long id) {
        return purchaseManageMapper.selectById(id);
    }

    @Override
    public PageResult<PurchaseManageDO> getPurchaseManagePage(PurchaseManagePageReqVO pageReqVO) {
        return purchaseManageMapper.selectPage(pageReqVO);
    }

    @Override
    public int updateStatus(long id, Integer status) {
        validatePurchaseManageExists(id);
        PurchaseManageDO purchaseManageDO = new PurchaseManageDO();
        purchaseManageDO.setId(id);
        purchaseManageDO.setStatus(status);
        return purchaseManageMapper.updateById(purchaseManageDO);
    }

}